package com.pur.zsms.common.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.util.ApplicationUtil;
import com.pur.zsms.querycode.service.IQueryCodeService;
import com.pur.zsms.systemsetting.datadictionary.po.Code;


/*
 * 自定义标签，根据字段类型和ID获取字段名称
 * @author
 */
public class CodeTag extends TagSupport  {
	/*public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		
		String codeName = "";
 		IQueryCodeService queryCodeService = (IQueryCodeService) ApplicationUtil.getBean("queryCodeService");
 		List<Code> codeList = null;
 		try {
 			codeList = queryCodeService.listCodeByCodeTypeId((String) codeTypeId);
		} catch (GenericException e) {
 		}
 		if(codeList != null){
 			for(Code code : codeList){
 				if(codeId.equals(code.getCodeId())){
 					codeName = code.getCodeName();
 					break;
 				}
 			}
 		}
 		//this.getJspContext().getOut().write(codeName);
 		JspWriter out = pageContext.getOut();
 		try {
			out.print(codeName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return super.doStartTag();
	}*/
	
	private String codeTypeId;
	private String codeId;
	/*@Override
	public void doTag() throws JspException, IOException {
 		super.doTag();
  		String codeName = "";
 		IQueryCodeService queryCodeService = (IQueryCodeService) ApplicationUtil.getBean("queryCodeService");
 		List<Code> codeList = null;
 		try {
 			codeList = queryCodeService.listCodeByCodeTypeId(codeTypeId);
		} catch (GenericException e) {
 		}
 		if(codeList != null){
 			for(Code code : codeList){
 				if(codeId.equals(code.getCodeId())){
 					codeName = code.getCodeName();
 					break;
 				}
 			}
 		}
 		this.getJspContext().getOut().write(codeName);

	}*/
	@Override
	public int doStartTag() {
		// TODO Auto-generated method stub
		String codeName = "";
 		//IQueryCodeService queryCodeService = (IQueryCodeService) ApplicationUtil.getBean("queryCodeService");
		IQueryCodeService queryCodeService = (IQueryCodeService)  ApplicationUtil.getBean("queryCodeService");
 		List<Code> codeList = null;
 		try {
 			codeList = queryCodeService.listCodeByCodeTypeId((String) codeTypeId);
		} catch (GenericException e) {
 		}
 		if(codeList != null){
 			for(Code code : codeList){
 				if(codeId.equals(code.getCodeId())){
 					codeName = code.getCodeName();
 					break;
 				}
 			}
 		}
 		//this.getJspContext().getOut().write(codeName);
 		JspWriter out = pageContext.getOut();
 		try {
			out.print(codeName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	public String getCodeTypeId() {
		return codeTypeId;
	}
	public void setCodeTypeId(String codeTypeId) {
		this.codeTypeId = codeTypeId;
	}
	public String getCodeId() {
		return codeId;
	}
	public void setCodeId(String codeId) {
		this.codeId = codeId;
	}  
	
}
